package top.xshiwu.demo.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import top.xshiwu.demo.service.MailService;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class MailServiceImpl implements MailService {

    private Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);

    @Resource
    JavaMailSender javaMailSender;

    @Value("${to.sender}")
    String sender;


    /**
     * 功能描述：发送带图片的邮件
     *
     * @param to      发送目标邮箱
     * @param subject 邮件标题
     * @param content 邮件内容
     * @param imgPath 图片路径
     * @param imgId   图片id，在img标签里使用
     * @throws MessagingException the messaging exception
     * @author: ***
     * @createTime: 2018 /11/8 9:18
     */
    @Override
    public void sendMail(String[] to, String subject, String content, String imgPath, String imgId) {
        //创建message
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            //发件人
            helper.setFrom(sender);
            //收件人
            helper.setTo(to);
            //标题
            helper.setSubject(subject);
            //true指的是html邮件，false指的是普通文本
            helper.setText(content, true);
            //添加图片
            FileSystemResource file = new FileSystemResource(new File(imgPath));
            helper.addInline(imgId, file);
            //发送邮件
            javaMailSender.send(message);
        } catch (MessagingException e) {
            logger.error("静态资源发送失败", e);
        }
    }

    /**
     * 带图片的邮件
     *
     * @param to      接受者QQ邮箱
     * @param subject 邮件标题
     * @param content 邮件内容
     * @param images  邮件里面带的图片
     */
    @Override
    public void sendMail(String[] to, String subject, String content, String images) {
        MimeMessage message = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            // 避免被当成垃圾邮件处理
            message.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
            // 邮件标题
            helper.setSubject(subject);
            // 内容
            helper.setText("<span style='colorL#ff6700'>" + content + "</span>", true);
            // 邮件发送方
            helper.setFrom(sender);
            // 邮件接收地址
            helper.setTo(to);
            // 图片
            FileSystemResource resource = new FileSystemResource(new File(images));
            helper.addInline(images, resource);

            javaMailSender.send(message);

        } catch (MessagingException e) {
            logger.error("静态资源发送失败", e);
        }
    }

    /**
     * 在附件的邮件发送
     *
     * @param to       接收人
     * @param subject  邮件标题
     * @param content  邮件内容
     * @param filename 附件名
     * @param filepath 附件地址
     * @throws MessagingException
     */
    @Override
    public void sendMailPath(String[] to, String subject, String content, String filename, String filepath) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");

        // 避免被当成垃圾邮件处理
        mimeMessage.addHeader("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
        // 邮件标题
        helper.setSubject(subject);
        // 内容
        helper.setText("<span style='colorL#ff6700'>" + content + "</span>", true);
        // 附件
        helper.addAttachment(filename, new File(filepath));

        helper.setFrom(sender);
        // 邮件接收地址
        helper.setTo(to);
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void sendMail(String[] to, String subject, String content) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码，true表示为multipart邮件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        // 邮件标题
        helper.setSubject(subject);
        //内容，可以用html设置样式，但是必须是true，默认是false
        helper.setText("<b><h1 style='color:#ff6700;'>" + content + "</h1></b>", true);
        //附件，filename：在邮件中展示附件的名字，filepath在当前机器中的路径
        helper.setFrom(sender);
        // 邮件接收地址
        helper.setTo(to);
        javaMailSender.send(mimeMessage);
    }
}
